<%@ page import="java.sql.*" %>
<%@ page import="com.example.demo_text.databaseaccess" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>游客课程展示</title>
    <style>
        /* 全局样式 */
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft Yahei', sans-serif;
            background: linear-gradient(135deg, #e0f7fa, #f3e5f5);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px 20px;
            color: #333;
        }

        .card {
            background: white;
            width: 100%;
            max-width: 300px;
            padding: 30px 25px;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
        }
F

        .course-list {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            margin-top: 1rem;
        }

        .course-item {
            padding: 15px;
            background: #f3f4f6;
            border-radius: 6px;
            text-align: center;
            transition: all 0.3s;
            cursor: pointer;
        }

        .course-item:hover {
            background: #e5e7eb;
            transform: translateY(-2px);
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        a {
            color: #15576b;
            text-decoration: none;
            display: block;
        }
    </style>
    <script>
        function showLoginPrompt() {
            alert("请先登录以查看课程详情！");
        }
    </script>
</head>
<body>

<div class="card">
    <h1>所有课程展示</h1>

    <div class="course-list">
        <%
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
              final String JDBC_URL = databaseaccess.DatabaseUrl2;
                 final String JDBC_USER = databaseaccess.DatabaseUser2;
                 final String JDBC_PASSWORD =  databaseaccess.DatabasePassword2;

                conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
                stmt = conn.createStatement();
                rs = stmt.executeQuery("SELECT course_id, course_name FROM course");

                while (rs.next()) {
                    String courseId = rs.getString("course_id");
                    String courseName = rs.getString("course_name");
        %>
        <div class="course-item" onclick="showLoginPrompt()">
            <%= courseName %>
        </div>
        <%
            }
        } catch (Exception e) {
        %>
        <p style='color:red;'>加载课程失败：<%= e.getMessage() %></p>
        <%
            } finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        %>
    </div>

    <!-- 返回主页按钮 -->
    <a href="index.jsp" class="btn-add-course">返回主页</a>
</div>

</body>
</html>
